package com.wzh.product.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.wzh.common.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wzh.product.entity.MydisEntity;
import com.wzh.product.service.MydisService;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author wzh
 * @email 1281204121@qq.com
 * @date 2022-01-22 21:07:56
 */
@RestController
@RequestMapping("product/mydis")
public class MydisController {
    @Autowired
    private MydisService mydisService;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,HttpServletRequest request){
        String token = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(token);
        params.put("tel",claims.getId());
        PageUtils page = mydisService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		MydisEntity mydis = mydisService.getById(id);

        return R.ok().put("mydis", mydis);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MydisEntity mydis, HttpServletRequest request){
        String token = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(token);
        mydis.setCreateDate(new Date());
        mydis.setEndDate(DateUtils.addDays(new Date(),mydis.getTian()));
        mydis.setState("未使用");
        mydis.setTel(claims.getId());
        return mydisService.add(mydis);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MydisEntity mydis){
		mydisService.updateById(mydis);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		mydisService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
